layui.define(['table', 'form', 'element'], function(exports){
  var $ = layui.$,table = layui.table,form = layui.form, element = layui.element;

  var activityId = $('input[name="id"]').val();

    table.render({
        elem: '#LAY-table'
        ,url: layui.setter.webRoot + '/business/coupon_activity/place/list.data'
        ,where:{activityId:activityId}
        ,method: 'post'
        ,parseData: function (res) {
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,size:'lg'
        ,cols: [[
            {field: 'placeId', title: '网点ID'}
            ,{field: 'placeName', title: '网点名称',templet:function (record){
                return record.place.name;
             }}
            ,{field: 'areaName', title: '网点所属区域',templet:function (record){
                    return record.place.areaName;
                }}
            ,{field: 'couponCount', title: '优惠次数'}
            ,{field: 'stateName', title: '状态'}
            ,{field: 'createDate', title: '创建时间'}
            ,{title: '操作', width: 120, align:'center', fixed: 'right', templet:function(record){
                var html = '';
                if(record.state == 2) {
                    html = html + '<a class="layui-btn layui-btn-xs" lay-event="open">开启</a>';
                }else{
                    html = html + '<a class="layui-btn layui-btn-xs" lay-event="close">关闭</a>';
                }
                html = html + '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>';
                return html;
            }}
        ]]
        ,page: false
        ,height: 'full'
        ,text: '对不起，加载出现异常！'
    });

    element.on('tab(tab-info)', function (data) {
        if(data.index== 0){
            table.reload('LAY-table')
        }
    });

    //事件
    var active = {
        add:function (){
            parent.layer.open({
                type: 2
                ,title: '新增网点'
                ,content: layui.setter.webRoot + '/business/coupon_activity/place/add.html?activityId='+activityId
                ,maxmin: true
                ,area: ['90%', '90%']
                ,btn: ['取消']
                , end: function(){
                    location.reload();
                }
            });
        }
        ,open:function(obj){
            layer.confirm('确定要启用该网点吗？', function(index){
                $.ajax({
                    type: "post",
                    url: layui.setter.webRoot + '/business/coupon_activity/place/open.do',
                    data:{activityId:activityId,placeId:obj.data.placeId},
                    dataType:"json",
                    success:function(res){
                        if(res.code == 200){
                            table.reload('LAY-table');
                        }else{
                            layer.msg(res.msg,{icon: 5});
                        }
                    }
                });
                layer.close(index);
            });
        }
        ,close:function(obj){
            layer.confirm('确定要关闭该网点吗？', function(index){
                $.ajax({
                    type: "post",
                    url: layui.setter.webRoot + '/business/coupon_activity/place/close.do',
                    data:{activityId:activityId,placeId:obj.data.placeId},
                    dataType:"json",
                    success:function(res){
                        if(res.code == 200){
                            table.reload('LAY-table');
                        }else{
                            layer.msg(res.msg,{icon: 5});
                        }
                    }
                });
                layer.close(index);
            });
        }
        ,del:function(obj){
            layer.confirm('确定要移除该网点吗？', function(index){
                $.ajax({
                    type: "post",
                    url: layui.setter.webRoot + '/business/coupon_activity/place/del.do',
                    data:{activityId:activityId,placeId:obj.data.placeId},
                    dataType:"json",
                    success:function(res){
                        if(res.code == 200){
                            obj.del();
                        }else{
                            layer.msg(res.msg,{icon: 5});
                        }
                    }
                });
                layer.close(index);
            });
        }
    }

    $('.layui-btn.LAY-btn-fun').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    table.on('tool(LAY-table)', function(obj){
        var type = obj.event ;
        active[type] ? active[type].call(this,obj) : '';
    });

  exports('coupon_activity_info', {})
});